//index.js
//获取应用实例
const app = getApp()
var t = require('./../../utils/request.js');
var u = require('./../../utils/util.js');


//计数器
var interval = null;

//值越大旋转时间越长  即旋转速度
var intime = 50;

Page({
  data: {
    color: [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
    //9张奖品图片
    images: ['/images/item.png', '/images/item1.png', '/images/item.png', '/images/item1.png', '/images/item.png', '/images/item1.png', '/images/item.png', '/images/item1.png', '/images/item.png'],
    btnconfirm: '/images/dianjichoujiang.png',
    clickLuck: 'clickLuck',
    luckPosition: 0,
    prizeData1: [
      {
        img: './../../images/1.png',
        text: '谢谢惠顾'
      },
      {
        img: './../../images/2.png',
        text: '100元抵购券'
      },
      {
        img: './../../images/1.png',
        text: '谢谢惠顾'
      },
      {
        img: './../../images/3.png',
        text: '20元抵购券'
      },
      
      {
        img: './../../images/1.png',
        text: '谢谢惠顾'
      },
      {
        img: './../../images/7.png',
        text: '50元抵购券'
      },
      {
        img: './../../images/1.png',
        text: '谢谢惠顾'
      },
      {
        img: './../../images/10.png',
        text: '10元零件抵价券'
      }
    ],
    userInfo: null,
    recode: '',
    show: false,
    prize_text: ''
  },

  onLoad: function () {
    this.loadAnimation();
  },

  onShow: function () {
    t.toPost('/minip/user/info', {
      uid: wx.getStorageSync('userInfo').id
    }, (res) => {
      this.setData({
        userInfo: res.data
      })
      wx.setStorageSync('userInfo', res.data)
    })

    t.toPost('/minip/recode/data', {
    }, (res) => {
      this.setData({
        recode: res.data
      })
    })

  },

  input: function (e) {
    var data = e.detail.value;
    this.setData({
      luckPosition: data
    })
  },

  onClose: function () {
    this.setData({
      show: false
    })
  },


  //点击抽奖按钮
  clickLuck: function () {

    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = 'https://market.jackgroup.cn/zhong.mp3'
    innerAudioContext.onPlay(() => {
    })

    var e = this;

    //判断中奖位置格式
    if (e.data.luckPosition == null || isNaN(e.data.luckPosition) || e.data.luckPosition > 8) {
      wx.showModal({
        title: '提示',
        content: '请填写正确数值',
        showCancel: false,
      })
      return;
    }
    //模拟网络请求时间  设为两秒
    t.toPost('/minip/user/prize', {
      uid: wx.getStorageSync('userInfo').id,
      type: wx.getStorageSync('userInfo').is_ticket
    }, (res) => {
      console.log(res, 999)
      if (res.code == '000000') {
        e.stop(res.data)
        e.setData({
          btnconfirm: '/images/dianjichoujiang.png',
          clickLuck: 'clickLuck',
        })
        e.onShow()
      } else {
        wx.showToast({
          title: res.message,
        })
        clearInterval(interval);
        e.setData({
          btnconfirm: '/images/dianjichoujiang.png',
          clickLuck: 'clickLuck',
        })
        return;
      }
    })

    //设置按钮不可点击
    e.setData({
      btnconfirm: '/images/dianjichoujiangd.png',
      clickLuck: '',
    })
    //清空计时器
    clearInterval(interval);
    var index = 0;
    //循环设置每一项的透明度
    interval = setInterval(function () {
      if (index > 8) {
        index = 0;
        e.data.color[8] = 0.5
      } else if (index != 0) {
        e.data.color[index - 1] = 0.5
      }
      e.data.color[index] = 1
      e.setData({
        color: e.data.color,
      })
      index++;
    }, intime);




  },

  //也可以写成点击按钮停止抽奖
  // clickStop:function(){
  //   var stoptime = 2000;
  //   setTimeout(function () {
  //     e.stop(1);
  //   }, stoptime)
  // },

  stop: function (which) {
    var e = this;
    //清空计数器
    clearInterval(interval);
    //初始化当前位置
    var current = -1;
    var color = e.data.color;
    for (var i = 0; i < color.length; i++) {
      if (color[i] == 1) {
        current = i;
      }
    }
    //下标从1开始
    var index = current + 1;

    e.stopLuck(which, index, intime, 10);
  },


  /**
   * which:中奖位置
   * index:当前位置
   * time：时间标记
   * splittime：每次增加的时间 值越大减速越快
   */
  stopLuck: function (which, index, time, splittime) {
    var e = this;
    //值越大出现中奖结果后减速时间越长
    var color = e.data.color;
    setTimeout(function () {
      //重置前一个位置
      if (index > 8) {
        index = 0;
        color[8] = 0.5
      } else if (index != 0) {
        color[index - 1] = 0.5
      }
      //当前位置为选中状态
      color[index] = 1
      e.setData({
        color: color,
      })
      //如果旋转时间过短或者当前位置不等于中奖位置则递归执行
      //直到旋转至中奖位置
      if (time < 400 || index != which) {
        //越来越慢
        splittime++;
        time += splittime;
        //当前位置+1
        index++;
        e.stopLuck(which, index, time, splittime);
      } else {
        //1秒后显示弹窗
        setTimeout(function () {
          var content = ""
          if (which == 1) { content = '谢谢惠顾' }
          if (which == 2) { content = '100元抵购券' }
          if (which == 3) { content = '谢谢惠顾' }
          if (which == 4) { content = '20元抵购券' }
          if (which == 5) { content = '谢谢惠顾' }
          if (which == 6) { content = '50元抵购券' }
          if (which == 7) { content = '谢谢惠顾' }
          if (which == 8) { content = '10元零件抵价券' }

          if (which == 1 || which == 3 || which == 5 || which == 7) {
            wx.showModal({
              title: '中奖提示',
              content: '很遗憾未中奖',
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            e.setData({
              show: true,
              prize_text: content
            })
          }
      

        }, 1000);
      }
    }, time);
  },
  //进入页面时缓慢切换
  loadAnimation: function () {
    var e = this;
    var index = 0;
    // if (interval == null){
    interval = setInterval(function () {
      if (index > 8) {
        index = 0;
        e.data.color[8] = 0.5
      } else if (index != 0) {
        e.data.color[index - 1] = 0.5
      }
      e.data.color[index] = 1
      e.setData({
        color: e.data.color,
      })
      index++;
    }, 500);
    // }  
  },
  /**
    * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: '不用剪线头的杰克A5，邀您9.9元抢500体验券！',
      path: '/pages/index/index',
      imageUrl: 'http://qny.zhiniugroup.com/blog/20190106162150.png',
      success: function (res) {
        console.log(res, '分享成功')
      }
    }
  }
})
